/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Discribe：编辑器模式下，在屏幕左下角显示Fps帧率
* CreateTime：2020-04-28 10:06:29
* Version：1.0
* Modify Recoder：
*************************************************/
using UnityEngine;

namespace TinyToolKit
{
    [DisallowMultipleComponent]
    public class ShowFPS : MonoBehaviour 
    {
        [Range(0, 1)]      
        public float refreshInterval = 0.2f;
        public Color color = Color.white;

        private float _timer;
        private int _frameCount;
        private string _fps;
        
        private void OnEnable()
        {
            _timer = 0;
            _frameCount = 0;
            _fps = string.Format("FPS: {0:F1}", 1f / Time.unscaledDeltaTime);
        }

        private void Update()
        {
            _timer += Time.unscaledDeltaTime;
            _frameCount++;

            if (_timer >= refreshInterval)
            {
                _fps = string.Format("FPS: {0:F1}", _frameCount / _timer);

                _timer = 0;
                _frameCount = 0;
            }
        }

        private void OnGUI()
        {
            var oriColor = GUI.color;
            GUI.color = color;
            GUI.Label(new Rect(10, Screen.height - 25, 68, 20), _fps);
            GUI.color = oriColor;
        }
    } 
} 